<?php

namespace sixteen::dom;

require_once('sixteen/dom/element.php');
require_once('sixteen/dom/elements/label.php');
require_once('sixteen/dom/elements/span.php');

class field extends sixteen::dom::element
{
	protected $isField = true;
	protected $label;
	protected $validation;
	protected $validationErrors = array();
	
	function __construct()
	{
		parent::__construct();
		self::initAttributes();
	}
	
	function initAttributes()
	{
		$this->attributes = array_merge(
			$this->attributes,
			array(
				'name' => null,
				'accesskey' => null,
				'disabled' => null,
				'label' => null,
				'accept-charset' => null
			)
		);
	}
	
	function renderLabel()
	{
		if($this->label)
		{
			$labelObject = new sixteen::dom::elements::label();
			$labelObject->setInnerHTML($this->label);
			$labelObject->setAttribute('for', $this->attributes['id']);
			return $labelObject->render();
		}
		else
		{
			return null;
		}
	}
	
	function setLabel($label=null)
	{
		$this->label = $label;
		return true;
	}
	
	function getLabel()
	{
		return $this->label;
	}
	
	function removeLabel()
	{
		if($this->label)
		{
			$this->label = null;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function setValidation($key, $value=null)
	{
		$this->validation[$key] = $value;
		return true;
	}
	
	function removeValidation($key)
	{
		$this->validation[$key] = null;
		unset($this->validation[$key]);
		return true;
	}
	
	function render()
	{
		if($this->label)
		{
			return self::renderLabel() . parent::render() . self::renderValidationErrors();
		}
		else
		{
			return parent::render() . self::renderValidationErrors();
		}
	}
	
	function renderValidationErrors()
	{
		if($this->validationErrors)
		{
			$markup = null;
			foreach($this->validationErrors as $key=>$value)
			{
				$markup .= $key.'-';
			}
			$errorElement = new sixteen::dom::elements::span();
			$errorElement->setInnerHTML($markup);
			return $errorElement->render();
		}
		else
		{
			return false;
		}
	}
	
	function setStateFromValue($value)
	{
		return false;
	}
	
	function isValid()
	{
		if(isset($this->validation))
		{
			$this->validation = (is_array($this->validation)) ? $this->validation : array($this->validation=>$this->validation);
		
			foreach($this->validation as $key=>$value)
			{
				$valid = true;
				$matches = null;
				$key = $key ? $key : $value;
				switch ($key)
				{
					case 'required':
						$valid = (strlen($this->attributes['value'])>0) ? true : false;
						break;
					case 'min':
						$valid = (strlen($this->attributes['value'])>=$value) ? true : false;
						break;
					case 'max':
						$valid = (strlen($this->attributes['value'])<=$value) ? true : false;
						break;
					case 'email':
						$regex = '/^([a-z0-9])(([\-.]|[_]+)?([a-z0-9]+))*(@)([a-z0-9])((([-]+)?([a-z0-9]+))?)*((.[a-z]{2,3})?(.[a-z]{2,6}))$/i';
						preg_match($regex, $this->attributes['value'], $matches, PREG_OFFSET_CAPTURE);
						$valid = (count($matches)>0) ? true : false;
						break;
					case 'alpha':
						$regex = '/^[A-Za-z]*$/';
						preg_match($regex, $this->attributes['value'], $matches, PREG_OFFSET_CAPTURE);
						$valid = (count($matches)>0) ? true : false;
						break;
					case 'numeric':
						$regex = '/^[0-9]*$/';
						preg_match($regex, $this->attributes['value'], $matches, PREG_OFFSET_CAPTURE);
						$valid = (count($matches)>0) ? true : false;
						break;
					case 'alphanumeric':
						$regex = '/^[A-Za-z0-9]*$/';
						preg_match($regex, $this->attributes['value'], $matches, PREG_OFFSET_CAPTURE);
						$valid = (count($matches)>0) ? true : false;
						break;
					case 'regex':
						$regex = $this->attributes['value'];
						preg_match($regex, $this->attributes['value'], $matches, PREG_OFFSET_CAPTURE);
						$valid = (count($matches)>0) ? true : false;
						break;
					case 'regex':
						$regex = $this->attributes['value'];
						preg_match($regex, $this->attributes['value'], $matches, PREG_OFFSET_CAPTURE);
						$valid = (count($matches)>0) ? true : false;
						break;
					default:
				}
				if(!$valid)
				{
					$this->validationErrors[$key] = $value;
				}
			}
			if(count($this->validationErrors)>0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			return true;
		}
	}
	
}

?>